package edu.iss.demo.web.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import edu.iss.demo.exception.PublicHolidayNotFound;
import edu.iss.demo.model.PublicHoliday;
import edu.iss.demo.service.PublicHolidayService;


@Controller
@RequestMapping(value = "/Publicholiday")
public class PublicHolidayController {
	
	@Autowired
	private PublicHolidayService publicHolidayService;
	
	
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public ModelAndView newPublicHolidaypage() {
		ModelAndView mav = new ModelAndView("publicholiday-new", "publicHoliday",new PublicHoliday());
		return mav;
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView PublicHolidayListPage() {
		ModelAndView mav = new ModelAndView("publicholiday-list");
		List<PublicHoliday> publicHolidayList = publicHolidayService.findAll();
		System.out.println("size---->"+publicHolidayList.size());
		mav.addObject("publicHolidayList", publicHolidayList);
		return mav;
	}

	
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public ModelAndView createNewPublicHoliday(@ModelAttribute @Valid PublicHoliday publicHoliday,
			BindingResult result, final RedirectAttributes redirectAttributes) {

		if (result.hasErrors())
			return new ModelAndView("publicholiday-new");

		publicHolidayService.create(publicHoliday);
		
		ModelAndView mav = new ModelAndView();
		String message = "New Leave Type " + publicHoliday.getDate()
				+ " was successfully created.";

		mav.setViewName("AdminHome");

		redirectAttributes.addFlashAttribute("message", message);
		return mav;
	}
	
	
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public ModelAndView editPublicHolidayPage(@PathVariable Long id) {
		ModelAndView mav = new ModelAndView("publicholiday-edit");
		PublicHoliday publicHoliday= publicHolidayService.findById(id);
		mav.addObject("publicHoliday", publicHoliday);
		return mav;
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
	public ModelAndView editPublicHoliday(@ModelAttribute @Valid PublicHoliday publicHoliday,
			BindingResult result, @PathVariable Long id,
			final RedirectAttributes redirectAttributes) throws PublicHolidayNotFound {

		publicHoliday.setPublicHolidayid(id);
		if (result.hasErrors())
			return new ModelAndView("publicholiday-edit");

		ModelAndView mav = new ModelAndView("AdminHome");
		String message = "Student was successfully updated.";

		publicHolidayService.update(publicHoliday);

		redirectAttributes.addFlashAttribute("message", message);
		return mav;
	}
	
}
